<?php
namespace App\Service;
use App\Entity\Announce;
use App\Entity\Message;
use App\Entity\Task;
use App\Entity\User;
use App\Repository\AnnounceCategoryRepository;
use App\Repository\AnnounceRepository;
use App\Repository\MessageCategoryRepository;
use App\Repository\MessageRepository;
use App\Repository\UserRepository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

class AnnounceService extends ServiceEntityRepository
{
    const __EVENT_TASK_CREATE__         = 1;
    const __EVENT_TASK_HIRE__           = 3;
    const __EVENT_TASK_CONFIRM__        = 4;
    const __EVENT_TASK_FINISH__         = 5;

    //支付参数设置
    public $shopService;
    public $userRepository;
    public $announceRepository;
    public $announceCategoryRepository;
    public $messageRepository;
    public $messageCategoryRepository;

    public function __construct(
        ManagerRegistry $registry
        ,ShopService $shopService
        ,UserRepository $userRepository
        ,AnnounceRepository $announceRepository
        ,AnnounceCategoryRepository $announceCategoryRepository
        ,MessageRepository $messageRepository
        ,MessageCategoryRepository $messageCategoryRepository
    )
    {
        parent::__construct($registry, Announce::class);
        $this->shopService = $shopService;
        $this->userRepository = $userRepository;
        $this->announceRepository = $announceRepository;
        $this->announceCategoryRepository = $announceCategoryRepository;
        $this->messageRepository = $messageRepository;
        $this->messageCategoryRepository = $messageCategoryRepository;
    }

    public function dealWithTaskCreate(Task $task)
    {
        $user = $task->getUser();
        $Announce = new Announce();
        $Announce->setCreateAt(new \DateTime())
            ->setEvent($this->announceCategoryRepository->find(self::__EVENT_TASK_CREATE__))
            ->setText($user->getName()." 发布了一条新的任务")
            ->setParam(array(
                "user"=>$user->getName()
            ))
        ;
        $this->_em->persist( $Announce);
        $this->_em->flush();
    }

    public function dealWithTaskHire(Task $task)
    {
        $user = $task->getUser();
        $Announce = new Announce();
        $Announce->setCreateAt(new \DateTime())
            ->setEvent($this->announceCategoryRepository->find(self::__EVENT_TASK_HIRE__))
            ->setParam(array(
                "user"=>$user->getName()
            ))
        ;
        $this->_em->persist( $Announce);
        $this->_em->flush();
    }

    public function dealWithTaskConfirm(User $user,$money)
    {
        $Announce = new Announce();
        $Announce->setCreateAt(new \DateTime())
            ->setEvent($this->announceCategoryRepository->find(self::__EVENT_TASK_CONFIRM__))
            ->setText("{$user->getName()}完成任务赚到{$money}元佣金")
            ->setParam(array(
                "user"=>$user->getName()
                ,"money"=>$money
            ))
        ;
        $this->_em->persist( $Announce);
        $this->_em->flush();
    }


}